package unitTests;

import static org.junit.Assert.*;

import java.util.Vector;

import org.junit.Before;
import org.junit.Test;

import reactor.Reactor;

import forumSystem.Forum;
import forumSystem.ForumRealDB;
import forumSystem.SubForum;

public class TestRealDB {
	String adminUser;
	String adminPass;
	ForumRealDB db;

	@Before
	public void setUp() throws Exception {
		adminUser="admin";
		adminPass="pass";
		db = new ForumRealDB();
		db.initialize(adminUser, adminPass) ;
	}

	@Test
	public void testAddForum() {
		int forumID = db.addForum("Games", "Fifa is the best !");
		Forum forum = db.getForumByID(forumID);
		assertEquals(forum.getName(), "Games");
		assertEquals(forum.getDesc(), "Fifa is the best !");
	}

	@Test
	public void testAddSubForum() {
		int forumID = db.addForum("Games", "Fifa is the best !");
		int subforumID = db.addSubForum("Online games", "Play online with friends and lose", forumID, 1);
		SubForum subforum = db.getSubForumByID(subforumID);
		assertEquals(subforum.getTitle(), "Online games");
		assertEquals(subforum.getDesc(), "Play online with friends and lose");
	}

	@Test
	public void testGetForumlist() {
		int forumID1 = db.addForum("Games", "Fifa is the best !");
		int forumID2 = db.addForum("Programing", "so much fun !");
		Vector<Forum> forums = db.getForumlist();
		assertEquals(forums.get(0).getId(), forumID1);
		assertEquals(forums.get(1).getId(), forumID2);
	}

	@Test
	public void testGetSubForumModerators() {
		int forumID = db.addForum("Games", "Fifa is the best !");
		int subforumID = db.addSubForum("Online games", "Play online with friends and lose", forumID, 1);
		int userId1 = db.addUser("or", "hemi", "orhemi@walla.com", forumID);
		int userId2 = db.addUser("hemi", "or", "orhemi2@walla.com", forumID);
		db.addModerateToSubForum(subforumID, userId1);
		db.addModerateToSubForum(subforumID, userId2);
		Vector<Integer> mods = db.getSubForumModerators(subforumID);
		assertTrue(mods.get(1) == userId1);
		assertTrue(mods.get(2) == userId2);
	}

	@Test
	public void testUserExist() {
		boolean ans = db.userExist("or");
		assertFalse(ans);
		int forumID = db.addForum("Games", "Fifa is the best !");
		int userId1 = db.addUser("or", "hemi", "orhemi@walla.com", forumID);
		ans = db.userExist("or");
		assertTrue(ans);
	}

	@Test
	public void testIsModerator() {
		int forumID = db.addForum("Games", "Fifa is the best !");
		int subforumID = db.addSubForum("Online games", "Play online with friends and lose", forumID, 1);
		int userId1 = db.addUser("or", "hemi", "orhemi@walla.com", forumID);
		int userId2 = db.addUser("hemi", "or", "orhemi2@walla.com", forumID);
		db.addModerateToSubForum(subforumID, userId1);
		int ans = db.isModerator(userId1, subforumID);
		assertTrue(ans == 1);
		ans = db.isModerator(userId2, subforumID);
		assertFalse(ans == 1);
	}

	@Test
	public void testGetParentForumID() {
		int forumID = db.addForum("Games", "Fifa is the best !");
		int subforumID = db.addSubForum("Online games", "Play online with friends and lose", forumID, 1);
		int ans = db.getParentForumID(subforumID);
		assertTrue(ans == forumID);
		
		ans = db.getParentForumID(subforumID+50);
		assertFalse(ans == forumID);
	}
	
	@Test
	public void testGetSubForumLastPost(){
		int forumID = db.addForum("Games", "Fifa is the best !");
		int subforumID = db.addSubForum("Online games", "Play online with friends and lose", forumID, 1);
		int userID = db.addUser("or", "hemi", "orhemi@walla.com", forumID);
		int post = db.addPost(userID, subforumID, "hi there", "whats up?");
		String ans = db.getSubForumLastPost(subforumID);
		System.out.println(ans);
	}
	
	@Test
	public void testRemoveForum(){
		int forumID = db.addForum("Games", "Fifa is the best !");
		int flag=Reactor.db.removeForum(forumID);
		assertFalse(flag==-1);
		flag=Reactor.db.removeForum(forumID);
		assertTrue(flag==-1);
	}
	
	@Test
	public void testGetForumByID(){
		int forumID = db.addForum("Games", "Fifa is the best !");
		Forum f=Reactor.db.getForumByID(forumID);
		assertEquals("Games", f.getName());
		assertEquals("Fifa is the best !", f.getDesc());
		assertEquals(1, f.getAdminID());
	}

}
